package com.sky.web.admin;

import com.sky.dto.EmployeeDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * 员工管理
 */
@Slf4j
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    JwtProperties jwtProperties;

    /**
     * 后台登录
     * @param employeeDTO
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody EmployeeDTO employeeDTO){
        //调用service
        Employee employee = employeeService.login(employeeDTO);
        //创建token, token 里面要保存empId
        HashMap<String, Object> cliams = new HashMap<>();
        cliams.put("empId",employee.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecret(),
                jwtProperties.getAdminTtl(),
                cliams);
        //返回结果
        EmployeeLoginVO loginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();
        //返回结果
        return Result.success(loginVO);
    }

    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    public Result logout(){
        return Result.success();
    }

    /**
     * 新增员工
     * @param employeeDTO
     * @return
     */
    @PostMapping
    public Result add(@RequestBody EmployeeDTO employeeDTO){
        employeeService.add(employeeDTO);
        return Result.success();
    }

}
